import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { TArticleComment } from 'src/entities/article-comment.entity';

@Injectable()
export class ArticleCommentService {
  constructor(
    @InjectRepository(TArticleComment)
    private readonly commentRepository: Repository<TArticleComment>,
  ) {}

  async addComment(comment: Partial<TArticleComment>) {
    const entity = this.commentRepository.create(comment);
    return await this.commentRepository.save(entity);
  }

  async getCommentsByArticle(articleId: number) {
    return await this.commentRepository.find({
      where: { articleId },
      order: { createdAt: 'DESC' },
    });
  }
}
